<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>债券设置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 注意：项目正式环境请勿引用该地址 -->
    <link rel="stylesheet" href="layui/css/layui.css"/>
    <script type="text/javascript" src="layui/layui.js"></script>
    <script type="text/javascript" src="layui/tableSelect.js"></script>
</head>
<body>

<!--表格上方的 工具条-->
<script type="text/html" id="bondToolBar">
    <button class="layui-btn layui-btn-sm" lay-event="insert"><i
            class="layui-icon">&#xe654;</i>新增</button>
    <button class="layui-btn layui-btn-sm" lay-event="deleteAll">批量删除</button>
    <div style="width:685px;float:right;height:20px;">
        <div class="layui-inline layui-show-xs-block">
            <label class="layui-form-label layui-bg-gray" style="width: 110px;text-align: center">计息起始日</label>
            <div class="layui-inline layui-show-xs-block">
                <input class="layui-input"  autocomplete="off" placeholder="请输入选择计息起始日" name="bearingStart" id="bearingStart">
            </div>
        </div>
        <div class="layui-inline layui-show-xs-block">
            <label class="layui-form-label layui-bg-gray" style="width: 110px;text-align: center">债券代码</label>
            <div class="layui-input-inline">
                <input type="text" name="bondId" lay-verify="required" id="bondId2"
                       autocomplete="off" placeholder="请输入债券代码" class="layui-input">
            </div>
        </div>
        <div class="layui-inline layui-show-xs-block" >
            <div class="layui-inline" style="float:right;height:29px;" title="搜索" lay-event="search"><i
                    class="layui-icon layui-icon-search"></i>
            </div>
        </div>


    </div>


</script>
<script>
    layui.use(['form', 'table', 'layer','laydate'], function(){
        var table = layui.table;
        var $  = layui.$;//jquery
        var layer= layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;

        laydate.render({
            elem: '#date'
        });

        laydate.render({
            elem: '#date2'
        });
        laydate.render({
            elem: '#date3'
        });
        laydate.render({
            elem: '#date4'
        });

        //查询
        table.render({
            elem: '#bondSetting',
            url:'/selectBondSetting', //数据接口
            height: 'full-55'
            , minLength: 80,
            page: true, //开启分页
            toolbar: '#bondToolBar',
            limits:[2,4,6,8],
            cols: [[ //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'bondId', title: '债券ID', sort: true, fixed: 'left'}
                ,{field: 'bondName', title: '债券名称'}
                ,{field: 'bearingStart', title: '计息起始日'}
                ,{field: 'bearingEnd', title: '计息结束日'}
                ,{field: 'bondType', title: '债券类型',templet:function (item){
                        if (item.bondType=='1'){
                            return '银行间';
                        }else if (item.bondType=='2'){
                            return '非银行间';
                        }
                    }}
                ,{field: 'faceAmount', title: '票面金额(元)'}
                ,{field: 'bondInterest', title: '债券利息(%)'}
                ,{field: 'delayDate', title: '票面利率(%)'}
                ,{field: 'interestTime', title: '付息次数',templet:function (item) {
                        if (item.interestTime=='1'){
                            return '一年一次';
                        }else if (item.interestTime=='2'){
                            return '一年两次';
                        }else if (item.interestTime=='3'){
                            return '一年四次';
                        }
                    }}
                ,{field: 'bondRemark', title: '债券备注'}
                ,{fixed: 'right', align:'center', toolbar: '#barDemo' ,width:200}//编辑删除的按钮
            ]]
        });

        laydate.render({
            elem: '#bearingStart'
        });
        // //给工具栏绑定事件
        table.on('toolbar(bondSetting)',function (obj) {
            //根据事件对象 来判定点击的是哪一个案例
            if(obj.event=='insert'){
                // alert('增加事件触发');
                //iframe层
                layer.open({
                    type: 1,
                    title: '添加债券信息',
                    closeBtn: 1,
                    move:false,
                    area : [ '66%',
                        '550px' ],
                    content:$("#addContent")
                });
            }else if(obj.event=='deleteAll'){
                alert('删除事件触发');
            }else if(obj.event=='search'){
                // alert('根据条件查询')
                var bondId=$("#bondId2").val();
                var bearingStart=$("#bearingStart").val();
                //表格的重新加载事件
                table.reload('bondSetting', {
                    method: 'post'
                    , where: {
                        'bondId': bondId,
                        'bearingStart': bearingStart,
                    }
                    , page: {
                        curr: 1
                    }
                });
                laydate.render({
                    elem: '#bearingStart' //指定元素
                });
                $("#bearingStart").val(bearingStart);
                $("#bondId2").val(bondId);
            }
        });
        //给最右边的列 添加事件
        table.on('tool(bondSetting)',function (obj) {
            switch (obj.event) {
                case "edit":
                    //得到选中行的数据
                    var data=obj.data;
                    //JSON.stringify(data)  拼接成JSON格式的字符串
                    var bookObj=$.parseJSON(JSON.stringify(data));
                    //弹出一个修改的窗口
                    form.val("editform",bookObj);
                    layer.open({
                        type:1, //iframe 内嵌窗口
                        title: '修改债券设置信息',
                        area:['66%','550px'],
                        content: $("#editContent")
                    });
                    break;
                case "del":
                    // alert("这是删除的操作");
                    // alert("选中的行：" + obj.data.bondId);
                    layer.confirm('您确定要删除吗?',function (index) {
                        layer.close(index);
                        //发送请求到服务器执行删除操作
                        $.post("deleteBondSetting/" + obj.data.bondId,
                            function () {
                                //回调函数，刷新表格数据
                                table.reload('bondSetting');
                            });
                    });
                    break;
            }
        });
        //给增加的form绑定提交事件
        form.on('submit(addsubmit)',function () {
            // alert("新增表单提交");
            //发送ajax请求到 服务器，
            //得到表单的所有数据
            var formData= form.val("addform");
            // alert('formData=' + formData);
            $.post(
                "insertBondSetting",
                formData,
                function(msg) {
                    //重新刷新表格
                    //给予提示信息，增加成功
                    // alert(msg);
                    if(msg==0){
                        layer.closeAll();
                        layer.msg('添加失败',{
                            title : '提示',
                            area : [ '200px',
                                '140px' ],
                            time : 0,
                            btn : [ '知道了' ]
                        });
                    }else {
                        table.reload('bondSetting');
                        layer.closeAll();
                        // layer.msg('添加成功',{
                        //     title : '提示',
                        //     area : [ '200px',
                        //         '140px' ],
                        //     time : 0,
                        //     btn : [ '知道了' ]
                        // });
                    }
                });
            $("#addform")[0].reset();
            return false;
        });
        //给修改的form绑定提交事件
        form.on('submit(editform)',function () {
            //发送ajax请求到 服务器，
            //得到表单的所有数据
            var formData= form.val("editform");
            // alert('formData=' + formData);
            $.post(
                "updateBondSetting",
                formData,
                function(msg) {
                    //重新刷新表格
                    //给予提示信息，修改成功
                    // alert(msg);
                    if(msg==0){
                        alert('修改失败');
                    }else {
                        alert('修改成功');
                    }
                });

        });
        //下拉表格
        layui.use(['table', 'form', 'tableSelect'], function () {
            var $ = layui.jquery,
                tableSelect = layui.tableSelect;
            //新增债券id下拉表格
            tableSelect.render({
                elem: '#bondId',
                checkedKey: 'securityId',
                table: {
                    url: 'selectSecurity?securityType=2',
                    cols: [
                        [
                            {type: 'radio', fixed: 'left'},
                            {field: 'securityId', title: '证券ID', sort: true, fixed: 'left',hidden:true}
                            ,{field: 'securityName', title: '证券名称'}
                        ]]
                },
                done: function (elem, data) {
                    var NEWJSON = [];
                    layui.each(data.data, function (index, item) {
                        NEWJSON.push(item.securityId)
                        // NEWJSON.push(item.securityName)
                        $("#hiddenBondId").val(item.securityId)
                        $("#hiddenBondName").val(item.securityName)
                    });
                    elem.val(NEWJSON.join(","))
                }
            });
        });
    });
</script>

<!--表格-->
<table id="bondSetting" lay-filter="bondSetting"></table>
<!--表格最后一列的操作按钮的工具栏-->
<div style="display: none;" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i
            class="layui-icon">&#xe642;</i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
            class="layui-icon">&#xe640;</i>删除</a>
</div>

<!--增加的div内容-->
<div id="addContent"
     style="display: none; height: 100%; height: 100%; text-align: center;">
    <form id="addform" lay-filter="addform"
          class="layui-form layui-form-pane"
          style="margin: 30px auto; display: inline-block;">
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">证券ID</label>
            <div class="layui-input-inline">
                <input type="text" lay-verify="required" id="bondId"
                       autocomplete="off" placeholder="请输入证券ID" class="layui-input" name="bondId">
                <input type="hidden" name="securityId" id="hiddenBondId">
            </div>
            <label class="layui-form-label layui-bg-gray" style=" width: 140px;margin-left: 50px;">证券名称</label>
            <div class="layui-input-inline">
                <input type="text" lay-verify="required" name="bondName" id="hiddenBondName"
                       autocomplete="off" placeholder="请输入证券名称" class="layui-input"
                       onkeyup="value=value.replace(/[\d]/g,'') "
                       onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))">
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">计息起始日</label>
            <div class="layui-input-inline">
                <input type="text" lay-verify="required" id="date3"
                       autocomplete="off" placeholder="请输入计息起始日" class="layui-input" name="bearingStart">
            </div>
            <label class="layui-form-label layui-bg-gray" style=" width: 140px;margin-left: 50px;">计息结束日</label>
            <div class="layui-input-inline">
                <input type="text" lay-verify="required" name="bearingEnd" id="date4"
                       autocomplete="off" placeholder="请输入计息结束日" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">债券类型</label>
            <div class="layui-input-inline">
                <select name="bondType" >
                    <<option placeholder="请选择"></option>
                    <option value="1">银行间</option>
                    <option value="2">非银行间</option>
                </select>
            </div>
            <label class="layui-form-label layui-bg-gray" style=" width: 140px;margin-left: 50px;">票面金额</label>
            <div class="layui-input-inline">
                <input type="number" lay-verify="required" name="faceAmount"
                       autocomplete="off" placeholder="请输入票面金额" class="layui-input"
                       >
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">债券利息</label>
            <div class="layui-input-inline">
                <input type="number" oninput="if(value.length>4)value=value.slice(0,4)" lay-verify="required"
                       autocomplete="off" placeholder="债券利息" class="layui-input" name="bondInterest">
            </div>
            <label class="layui-form-label layui-bg-gray" style=" width: 140px;margin-left: 50px;">票面利率</label>
            <div class="layui-input-inline">
                <input type="number" oninput="if(value.length>4)value=value.slice(0,4)" lay-verify="required" name="delayDate" style="width:120px;"
                       autocomplete="off" placeholder="票面利率" class="layui-input"
                >
                <div style="margin-top: -27px;margin-left: 125px;"><a style="font-size:18px;">%</a></div>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">付息次数</label>
            <div class="layui-input-inline">
                <select name="interestTime">
                    <option placeholder="请选择"></option>
                    <option value="1">1年一次</option>
                    <option value="2">1年两次</option>
                    <option value="3">1年四次</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px; height: 100px;line-height: 81px">债券备注</label>
            <div class="layui-input-inline">
                <div class="layui-input-inline">
					<textarea rows="" name="bondRemark" placeholder="请输入债券备注" cols=""
                              class="layui-textarea" style="width: 467px"></textarea>
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="position: absolute;left: 28%; bottom: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addsubmit"><i class="layui-icon">&#x1005;</i>添加</button>
                <button type="reset" class="layui-btn layui-btn-primary"> <i class="layui-icon">&#x1006;</i>取消</button>
            </div>
        </div>
    </form>
</div>
<!--修改的div内容-->
<div id="editContent"
     style="display: none; height: 100%; height: 100%; text-align: center;">
    <form id="editform" lay-filter="editform"
          class="layui-form layui-form-pane"
          style="margin: 30px auto; display: inline-block;">
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">证券ID</label>
            <div class="layui-input-inline">
                <input type="number" lay-verify="required" disabled
                       autocomplete="off" placeholder="请输入证券ID" class="layui-input" name="bondId">
            </div>
            <label class="layui-form-label layui-bg-gray" style=" width: 140px;margin-left: 50px;">证券名称</label>
            <div class="layui-input-inline">
                <input type="text" lay-verify="required" name="bondName" disabled
                       autocomplete="off" placeholder="请输入证券名称" class="layui-input"
                       onkeyup="value=value.replace(/[\d]/g,'') "
                       onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))">
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">计息起始日</label>
            <div class="layui-input-inline">
                <input type="text" lay-verify="required" id="date"
                       autocomplete="off" placeholder="请输入计息起始日" class="layui-input" name="bearingStart">
            </div>
            <label class="layui-form-label layui-bg-gray" style=" width: 140px;margin-left: 50px;">计息结束日</label>
            <div class="layui-input-inline">
                <input type="text" id="date2" lay-verify="required" name="bearingEnd"
                       autocomplete="off" placeholder="请输入计息结束日" class="layui-input"
                >
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">债券类型</label>
            <div class="layui-input-inline">
                <select name="bondType" >
                    <<option placeholder="请选择"></option>
                    <option value="1">银行间</option>
                    <option value="2">非银行间</option>
                </select>
            </div>
            <label class="layui-form-label layui-bg-gray" style=" width: 140px;margin-left: 50px;">票面金额</label>
            <div class="layui-input-inline">
                <input type="number" lay-verify="required" name="faceAmount"
                       autocomplete="off" placeholder="请输入票面金额" class="layui-input"
                >
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">债券利息</label>
            <div class="layui-input-inline">
                <input type="number" step="0.00001" lay-verify="required"
                       autocomplete="off" placeholder="请输入债券利息" class="layui-input" name="bondInterest">
            </div>
            <label class="layui-form-label layui-bg-gray" style=" width: 140px;margin-left: 50px;">票面利率</label>
            <div class="layui-input-inline">
                <input type="number" step="0.00001" style="width:120px;"  lay-verify="required" name="delayDate"
                       autocomplete="off" placeholder="请输入票面利率" class="layui-input"
                >
                <div style="margin-top: -27px;margin-left: 125px;"><a style="font-size:18px;">%</a></div>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px">付息次数</label>
            <div class="layui-input-inline">
                <select name="interestTime">
                    <option placeholder="请选择"></option>
                    <option value="1">1年一次</option>
                    <option value="2">1年俩次</option>
                    <option value="3">1年四次</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label layui-bg-gray" style="width: 140px; margin-left: 35px; height: 100px;line-height: 81px">债券备注</label>
            <div class="layui-input-inline">
                <div class="layui-input-inline">
					<textarea rows="" name="bondRemark" placeholder="请输入债券备注" cols=""
                              class="layui-textarea" style="width: 467px"></textarea>
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="position: absolute;left: 28%; bottom: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="editsubmit"><i class="layui-icon">&#x1005;</i>添加</button>
                <button type="reset" class="layui-btn layui-btn-primary"> <i class="layui-icon">&#x1006;</i>取消</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>